package com.suineg.pms.entity.base;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.google.common.collect.Lists;
import com.suineg.pms.entity.IdEntity;
import com.suineg.pms.entity.User;
import com.suineg.pms.entity.commons.SystemInfo;
import com.suineg.pms.entity.fee.FeePlan;

/**
 * 房屋实体
 *@author Suine
 */
@Entity
@Table(name = "pms_room")
public class Room extends IdEntity{
	/** 业主名 */
	private String personName;
	/** 房屋编号  */
	private String houseNo;
	/** 小区 */
	private Community community;
	/** 楼栋 */
	private Building building;
	/** 单元 */
	private String unit;
	/** 室号 */
	private String roomNo;
	/** 用途 */
	private SystemInfo used;
	/** 建设面积  */
	private Double buildArea;
	/** 实际面试 */
	private Double factArea;
	/** 房屋性质  */
	private SystemInfo houseNature;
	/** 购房日期 */
	private Date buyDate;
	/** 状态  */
	private SystemInfo status;
	/** 合同号 */
	private String contractNumber;
	/** 入住时间 */
	private Date comeInDate;
	/** 操作人 */
	private User opuser;
	/** 有无电梯  */
	private SystemInfo elevater;
	/** 操作时间 */
	private Date opdate;
	/** 楼栋类型  高层or 多层*/
	private SystemInfo buildType;
	/** 方案 */
	private List<FeePlan> plans = Lists.newArrayList();
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "pms_plan_room",joinColumns={@JoinColumn(name = "room_id")},inverseJoinColumns={@JoinColumn(name = "plan_id")})
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序
	@OrderBy("id ASC")
	// 缓存策略
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<FeePlan> getPlans() {
		return plans;
	}
	public void setPlans(List<FeePlan> plans) {
		this.plans = plans;
	}
	public Room() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Room(String personName, String houseNo, Community community, Building building, String unit, String roomNo, SystemInfo status, User opuser, SystemInfo elevater,
			Date opdate, SystemInfo buildTypeInfo) {
		super();
		this.personName = personName;
		this.houseNo = houseNo;
		this.community = community;
		this.building = building;
		this.unit = unit;
		this.roomNo = roomNo;
		this.status = status;
		this.opuser = opuser;
		this.elevater = elevater;
		this.opdate = opdate;
		this.buildType = buildTypeInfo;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	public SystemInfo getBuildType() {
		return buildType;
	}
	public void setBuildType(SystemInfo buildType) {
		this.buildType = buildType;
	}
	public String getPersonName() {
		return personName;
	}
	public void setPersonName(String personName) {
		this.personName = personName;
	}
	public String getHouseNo() {
		return houseNo;
	}
	public void setHouseNo(String houseNo) {
		this.houseNo = houseNo;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "community_id")
	public Community getCommunity() {
		return community;
	}
	public void setCommunity(Community community) {
		this.community = community;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="building_id")
	public Building getBuilding() {
		return building;
	}
	public void setBuilding(Building building) {
		this.building = building;
	}
	public String getUnit() {
		return unit;
	}
	public void setUnit(String unit) {
		this.unit = unit;
	}
	public String getRoomNo() {
		return roomNo;
	}
	public void setRoomNo(String roomNo) {
		this.roomNo = roomNo;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	public SystemInfo getUsed() {
		return used;
	}
	public void setUsed(SystemInfo used) {
		this.used = used;
	}
	public Double getBuildArea() {
		return buildArea;
	}
	public void setBuildArea(Double buildArea) {
		this.buildArea = buildArea;
	}
	public Double getFactArea() {
		return factArea;
	}
	public void setFactArea(Double factArea) {
		this.factArea = factArea;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	public SystemInfo getHouseNature() {
		return houseNature;
	}
	public void setHouseNature(SystemInfo houseNature) {
		this.houseNature = houseNature;
	}
	public Date getBuyDate() {
		return buyDate;
	}
	public void setBuyDate(Date buyDate) {
		this.buyDate = buyDate;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	public SystemInfo getStatus() {
		return status;
	}
	public void setStatus(SystemInfo status) {
		this.status = status;
	}
	public String getContractNumber() {
		return contractNumber;
	}
	public void setContractNumber(String contractNumber) {
		this.contractNumber = contractNumber;
	}
	public Date getComeInDate() {
		return comeInDate;
	}
	public void setComeInDate(Date comeInDate) {
		this.comeInDate = comeInDate;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	public User getOpuser() {
		return opuser;
	}
	public void setOpuser(User opuser) {
		this.opuser = opuser;
	}
	public Date getOpdate() {
		return opdate;
	}
	public void setOpdate(Date opdate) {
		this.opdate = opdate;
	
	}
	@ManyToOne(fetch = FetchType.LAZY)
	public SystemInfo getElevater() {
		return elevater;
	}
	public void setElevater(SystemInfo elevater) {
		this.elevater = elevater;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((building == null) ? 0 : building.hashCode());
		result = prime * result + ((houseNo == null) ? 0 : houseNo.hashCode());
		result = prime * result + ((roomNo == null) ? 0 : roomNo.hashCode());
		return result;
	}
	
	/**
	 * 如果。房屋编号  室号 楼栋一样 就视为同一个房屋
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Room other = (Room) obj;
		if (building == null) {
			if (other.building != null)
				return false;
		} else if (!building.equals(other.building))
			return false;
		if (houseNo == null) {
			if (other.houseNo != null)
				return false;
		} else if (!houseNo.equals(other.houseNo))
			return false;
		if (roomNo == null) {
			if (other.roomNo != null)
				return false;
		} else if (!roomNo.equals(other.roomNo))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Room [personName=" + personName + ", houseNo=" + houseNo + ", community=" + community + ", building=" + building + ", unit=" + unit + ", roomNo=" + roomNo
				+ ", used=" + used + ", buildArea=" + buildArea + ", factArea=" + factArea + ", houseNature=" + houseNature + ", buyDate=" + buyDate + ", status=" + status
				+ ", contractNumber=" + contractNumber + ", comeInDate=" + comeInDate + ", opuser=" + opuser + ", elevater=" + elevater + ", opdate=" + opdate + ", buildType="
				+ buildType + "]";
	}
}

